package com.example.cd5160866.modelrecycleview.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.cd5160866.modelrecycleview.ModelRecyclerView;
import com.example.cd5160866.modelrecycleview.R;
import com.example.cd5160866.modelrecycleview.adapter.ModelRecyclerAdapterV3;
import com.example.cd5160866.modelrecycleview.adapter.RecyclerItemViewId;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cd5160866 on 2016/10/8.
 */
public class Demo4Activity extends AppCompatActivity {
    @BindView(R.id.rv)
    ModelRecyclerView recyclerView;

    ModelRecyclerAdapterV3 adapter;

    protected List<String> data;

    private List<View> listHeader = new ArrayList<>();
    private List<View> listFooter = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_model);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ButterKnife.bind(this);
        initDada();
        init();
    }

    private void init() {
        adapter = new ModelRecyclerAdapterV3<>(this, MyImageViewHolder.class, data);
//        adapter.addHeaderView(new TextView(this));
//        adapter.addFooterView(new TextView(this));
        recyclerView.setAdapter(adapter);
        recyclerView.setOnItemClickListener(new ModelRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View itemView, int position) {
                Toast.makeText(getApplicationContext(), position + " 点击~", Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setOnItemLongClickListener(new ModelRecyclerView.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(RecyclerView parent, View itemView, int position) {
                Toast.makeText(getApplicationContext(), position + " 长按~", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_demo2, menu);
        return true;
    }

    private TextView getTestView() {
        TextView view = (TextView) LayoutInflater.from(this).inflate(R.layout.item_header_text, recyclerView, false);
        return view;
    }

    /**
     * 我们的实际使用中的viewhoder
     * 注解注入item的布局文件，在这个类的实现中，我们可以绑定点击事件，更新数据
     */
    @RecyclerItemViewId(R.layout.item_list)
    public static class MyImageViewHolder extends ModelRecyclerAdapterV3.ModelViewHolder<String> {
        @BindView(R.id.iv_item1)
        ImageView imageView;

        public int position;

        public MyImageViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void bindButterKnife(View convertView) {
            ButterKnife.bind(this, convertView);
        }

        @Override
        public void update(String item, ModelRecyclerAdapterV3 adapter, int position) {
            this.position = position;
            Picasso.with(adapter.getContext()).load(item).into(imageView);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.list_vertical:
                recyclerView.setLayoutModel(ModelRecyclerView.MODEL_LIST_VIEW_VERTICAL);
                break;
            case R.id.list_horizontal:
                recyclerView.setLayoutModel(ModelRecyclerView.MODEL_LIST_VIEW_HORIZONTAL);
                break;
            case R.id.gridview:
                recyclerView.setLayoutModel(ModelRecyclerView.MODEL_GRID_VIEW_VERTICAL);
                break;
            case R.id.add_header:
                TextView header = getTestView();
                listHeader.add(0, header);
                int id = adapter.addHeaderView(header);
                header.setText(id + "");
                break;
            case R.id.remove_header:
                if (listHeader.size() > 0) {
                    View view = listHeader.get(0);
                    adapter.removeHeaderView(view);
                    listHeader.remove(view);
                }
                break;
            case R.id.add_footer:
                TextView footer = getTestView();
                footer.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                listFooter.add(0, footer);
                int idFooter = adapter.addFooterView(footer);
                footer.setText(idFooter + "");
                break;
            case R.id.remove_footer:
                if (listFooter.size() > 0) {
                    View view = listFooter.get(0);
                    adapter.removeFooterView(view);
                    listFooter.remove(view);
                }
                break;
            default:
                break;
        }
        return true;
    }

    private void initDada() {
        data = new ArrayList<>();
        //        data.add("http://upload4.hlgnet.com/bbsupfile/2010/2010-12-26/20101226151451_39.jpg");
        data.add("http://img1.imgtn.bdimg.com/it/u=2816269382,3258126399&fm=23&gp=0.jpg");
        data.add("http://img.taopic.com/uploads/allimg/140318/240405-14031PH95829.jpg");
        data.add("http://img3.100bt.com/upload/ttq/20130413/1365830891087.jpg");
        data.add("http://img2.duitang.com/uploads/item/201201/16/20120116004745_THihZ.jpg");
        data.add("http://a1.att.hudong.com/28/57/20300534506289144593571229462_s.jpg");
        data.add("http://g.hiphotos.baidu.com/image/h%3D200/sign=ccb17e18d03f8794ccff4f2ee21a0ead/a8773912b31bb051a6ed2e88327adab44bede041.jpg");
    }

}
